/**
 * 顾客接口 
 */
var express = require('express');
var router = express.Router();
var sql = require('../db/index');

/**
 * @swagger
 * /guke/saveOrUpdate:
 *   post:
 *     description: 顾客新增或者修改接口
 *     tags: [顾客]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         description: 顾客id
 *         required: true
 *         type: number
 *       - name: username
 *         description: 用户名
 *         required: true
 *         type: string
 *       - name: realname
 *         description: 用户姓名
 *         required: true
 *         type: string
 *       - name: gender
 *         description: 性别
 *         required: true
 *         type: string
 *     responses:
 *       200:
 *         description: 操作成功
 *       500:
 *         description: 服务器错误
 */

// 新增或者修改接口  req.body
router.post('/saveOrUpdate', async (req, res) => {
  // 获取前端传递的参数
  const { id, username, realname, gender } = req.body;
  // 如果id存在 修改操作 否则就是新增
  let mysql = '';
  if (id) {
<<<<<<< HEAD
    mysql = `update guke set username = '${username}', realname = '${realname}', gender = '${gender}' where id = ${id}`;
  } else {
    mysql = `insert into guke (username, realname, gender) values ('${username}', '${realname}', '${gender}')`;
=======
    mysql = `update  base_user set username = '${username}', realname = '${realname}', gender = '${gender}' where id = ${id}`;
  } else {
    mysql = `insert into  base_user (username, realname, gender) values ('${username}', '${realname}', '${gender}')`;
>>>>>>> 80f8de9502714fe0507a7a8a7e182c7fd3cb8eaf
  }
  await sql.query(mysql);
  // 给前端做出响应
  res.send({
    status: 200,
    message: "操作成功",
    data: null,
    timestamp: new Date().getTime()
  });
});

/**
 * @swagger
 * /guke/changeStatus:
 *   post:
 *     description: 修改顾客状态接口
 *     tags: [顾客]
 *     parameters:
 *       - in: query
 *         name: id
 *         description: 顾客id
 *         required: true
 *         schema:
 *           type: number
 *       - in: query
 *         name: status
 *         description: 顾客状态
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       '200':
 *         description: 返回顾客列表
 */

// 修改顾客状态接口 req.body 需要前端传递参数 id status
router.post('/changeStatus', async (req, res) => {
  // 获取前端传递的参数
  let { id, status } = req.body;
  if (status === '停用') {
    status = '停用';
  } else {
    status = '正常';
  }
<<<<<<< HEAD
  await sql.query(`update guke set status = '${status}' where id = ${id}`);
=======
  await sql.query(`update base_user set status = '${status}' where id = ${id}`);
>>>>>>> 80f8de9502714fe0507a7a8a7e182c7fd3cb8eaf
  // 给前端做出响应
  res.send({
    status: 200,
    message: status == '停用' ? '停用成功' : '启用成功',
    data: null,
    timestamp: new Date().getTime()
  });
});

/**
 * @swagger
 * /guke/pageQuery:
 *   get:
 *     description: 分页多条件查询顾客列表
 *     tags: [顾客]
 *     parameters:
 *       - in: query
 *         name: page
 *         description: 页码
 *         required: true
 *         schema:
 *           type: number
 *       - in: query
 *         name: pageSize
 *         description: 每页显示数量
 *         required: true
 *         schema:
 *           type: number
 *       - in: query
 *         name: id
 *         description: 顾客id
 *         required: true
 *         schema:
 *           type: number
 *       - in: query
 *         name: username
 *         description: 用户名
 *         required: true
 *         schema:
 *           type: string
 *       - in: query
 *         name: realname
 *         description: 用户姓名
 *         required: true
 *         schema:
 *           type: string
 *       - in: query
 *         name: gender
 *         description: 性别
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       '200':
 *         description: 返回顾客列表
 */

// 分页模糊查询接口
router.get('/pageQuery', async (req, res) => {
  // 获取前端传递的参数
  let { page, pageSize, username, realname, gender } = req.query;
  page = +page;
  pageSize = +pageSize;
  let result, result1;
  // 每页查询条数
  let offset = (page - 1) * pageSize;

  if (username && realname && gender) {
<<<<<<< HEAD
    let mysql = `select * from guke where username like '%${username}%' and realname like '%${realname}%' and gender = '${gender}' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from guke where username like '%${username}%' and realname like '%${realname}%' and gender = '${gender}'`;
    result1 = await sql.query(mysql1);
  } else if (username && realname) {
    let mysql = `select * from guke where username like '%${username}%' and realname like '%${realname}%' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from guke where username like '%${username}%' and realname like '%${realname}%'`;
    result1 = await sql.query(mysql1);
  } else if (username && gender) {
    let mysql = `select * from guke where username like '%${username}%' and gender = '${gender}' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from guke where username like '%${username}%' and gender = '${gender}'`;
    result1 = await sql.query(mysql1);
  } else if (realname && gender) {
    let mysql = `select * from guke where realname like '%${realname}%' and gender = '${gender}' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from guke where gender = '${gender}'`;
    result1 = await sql.query(mysql1);
  } else {
    // 分页查询
    let mysql = `select * from guke limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from guke`;
=======
    let mysql = `select * from base_user where username like '%${username}%' and realname like '%${realname}%' and gender = '${gender}' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from  base_user where username like '%${username}%' and realname like '%${realname}%' and gender = '${gender}'`;
    result1 = await sql.query(mysql1);
  } else if (username && realname) {
    let mysql = `select * from  base_user where username like '%${username}%' and realname like '%${realname}%' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from  base_user where username like '%${username}%' and realname like '%${realname}%'`;
    result1 = await sql.query(mysql1);
  } else if (username && gender) {
    let mysql = `select * from  base_user where username like '%${username}%' and gender = '${gender}' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from   base_user where username like '%${username}%' and gender = '${gender}'`;
    result1 = await sql.query(mysql1);
  } else if (realname && gender) {
    let mysql = `select * from  base_user where realname like '%${realname}%' and gender = '${gender}' limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from base_user where gender = '${gender}'`;
    result1 = await sql.query(mysql1);
  } else {
    // 分页查询
    let mysql = `select * from  base_user limit ${pageSize} offset ${offset}`;
    result = await sql.query(mysql);
    let mysql1 = `select count(*) as total from  base_user`;
>>>>>>> 80f8de9502714fe0507a7a8a7e182c7fd3cb8eaf
    result1 = await sql.query(mysql1);
  }

  result = result.filter(item => item.length > 0);
  console.log(result[0], '2222');
  total = result1[0][0].total;
  
  res.send({
    status: 200,
    message: '查询成功',
    data: result[0],
    total,
    timestamp: new Date().getTime()
  });
});

module.exports = router;